package sjtu.ist.warehouse4ws.service;

import java.sql.SQLException;

import org.dom4j.DocumentException;

import sjtu.ist.warehouse4ws.dao.cust.CustColumnDaoHibernate;
import sjtu.ist.warehouse4ws.dao.cust.CustColumnValueDaoHibernate;
import sjtu.ist.warehouse4ws.dao.cust.CustDomainModelAttributeDaoHibernate;
import sjtu.ist.warehouse4ws.dao.cust.CustDomainModelDaoHibernate;
import sjtu.ist.warehouse4ws.dao.cust.CustDomainModelStateDaoHibernate;
import sjtu.ist.warehouse4ws.dynamicDS.MetadataExport;
import sjtu.ist.warehouse4ws.dynamicDS.MetadataImport;

public class CreateTableServiceImpl implements CreateTableService {
	CustColumnDaoHibernate custColumnDao;
	CustColumnValueDaoHibernate custColumnValueDao;
	CustDomainModelDaoHibernate custDomainModelDao;
	CustDomainModelAttributeDaoHibernate custDomainModelAttributeDao;
	CustDomainModelStateDaoHibernate custDomainModelStateDao;
	
	MetadataImport im=new MetadataImport();
	
	@Override
	public void createTable(String custName) {
		// TODO Auto-generated method stub
//		custColumnDao.createTable(custName);
//		custColumnValueDao.createTable(custName);
//		custDomainModelDao.createTable(custName);
//		custDomainModelAttributeDao.createTable(custName);
//		custDomainModelStateDao.createTable(custName);
		
		String[] tablenames={"domainmodel"
				,"domainmodelattr"
				,"domainmodelstate"
				,"epcunit"
				,"epcunits"
				,"fileinfo"
				,"functions"
				,"functionpoint"
				,"parameter"
				,"permission"
				,"permissionresources"
				,"process"
				,"resource"
				,"role"
				,"role_epc"
				,"rolepermissions"
				,"subprocess"
				,"column"
				,"column_value"
				,"modelgroup"
				,"department"
				,"functionpoint_epc"
				,"domain_epc"
				};
		for(int i=0;i<tablenames.length;i++){
			MetadataExport.exportxml(tablenames[i]);
		}
		try {
			Thread.sleep(50);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		for(int i=0;i<tablenames.length;i++){
			boolean suc=false;
			while(!suc){
				try {
					MetadataImport.executeSql(MetadataExport.exportpath+tablenames[i]+".xml",custName+"_cust_");
					suc=true;
				} catch (DocumentException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				}
			}
			
		}
	}

	public void setCustColumnDao(CustColumnDaoHibernate custColumnDao) {
		this.custColumnDao = custColumnDao;
	}

	public void setCustColumnValueDao(CustColumnValueDaoHibernate custColumnValueDao) {
		this.custColumnValueDao = custColumnValueDao;
	}

	public void setCustDomainModelDao(CustDomainModelDaoHibernate custDomainModelDao) {
		this.custDomainModelDao = custDomainModelDao;
	}

	public void setCustDomainModelAttributeDao(
			CustDomainModelAttributeDaoHibernate custDomainModelAttributeDao) {
		this.custDomainModelAttributeDao = custDomainModelAttributeDao;
	}

	public void setCustDomainModelStateDao(
			CustDomainModelStateDaoHibernate custDomainModelStateDao) {
		this.custDomainModelStateDao = custDomainModelStateDao;
	}

}
